package com.orangelife.mobile.confirmOrder.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import com.common.jump.PayJumper;
import com.curry.android.http.HttpUtil;
import com.curry.android.util.DialogHelper;
import com.curry.android.util.JSONHelper;
import com.curry.android.util.ToastHelper;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.app.application.CurryApplication;
import com.orangelife.mobile.app.application.ExitApplication;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.orangelife.mobile.common.biz.JSONRequest;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.constants.ConfirmOrderURL;
import com.orangelife.mobile.goodsrent.adapter.GoodsRentOrderAdapter;
import com.orangelife.mobile.goodsrent.dao.GoodsDao;
import com.orangelife.mobile.goodsrent.entity.GoodsInfo;
import com.orangelife.mobile.pay.biz.PaymentModule;
import com.orangelife.mobile.widget.ScrollListView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

public class GoodsRentConfirmOrderActivity extends OrangeLifeBaseActivity {
    /** 标题 */
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.lvGoodsRent)
    ScrollListView lvGoodsRent;
    @Bind(R.id.tvShippingMethod)
    TextView tvShippingMethod;
    @Bind(R.id.tvPayPrice)
    TextView tvPayPrice;
    @Bind(R.id.tvPay)
    TextView tvGoodsRentPay;
    @Bind(R.id.tv_userphone)
    TextView tvUserPhone;
    @Bind(R.id.tv_username)
    TextView tvUserName;
    @Bind(R.id.tv_address)
    TextView tvAddress;
    @Bind(R.id.tvSendMethod)
    TextView tvSendMethod;
    private String totalPrice;
    private GoodsRentOrderAdapter goodsRentOrderAdapter;
    private List<GoodsInfo> goodsInfoList;
    Map<String, Object> goodsMap = null;
    Map<String, Object> payMap = null;
    private static final int ADD_RENT_ORDER = 0;
    public static final String GOODS_INFO = "goodsInfo";
    private boolean isShopping;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_rent_confirm_order);
        ButterKnife.bind(this);
        setTitleText(tvTitle, getString(R.string.order_firm));
        setAdapter();
        setGoodsRentInfo();
        setOnClickListener();
    }

    private void setOnClickListener() {
        tvGoodsRentPay.setOnClickListener(listener);
    }

    private void setGoodsRentInfo() {
        switch (goodsInfoList.get(0).getDispatchType()) {
            case GoodsInfo.MAIL:
                tvShippingMethod.setText(getString(R.string.rent_mail));
                break;
            case GoodsInfo.DELIVERED:
                tvShippingMethod.setText(getString(R.string.rent_delivered));
                break;
            case GoodsInfo.RECEIPT:
                tvShippingMethod.setText("物业处自取");
                break;
        }
        tvSendMethod.setText(getString(R.string.postage));
    }

    private void addGoodsRentRequset() {
        showDialog(this);
        Map<String, Object> map = new HashMap<>();
        List<Map<String, Object>> goodsList = new ArrayList<>();
        for (int i = 0; i < goodsInfoList.size(); i++) {
            Map<String, Object> mapGoods = new HashMap();
            mapGoods.put("goodsID", goodsInfoList.get(i).getrGoodID());
            mapGoods.put("rentalDays", goodsInfoList.get(i).getDay());
            mapGoods.put("rentalNum", "1");
            goodsList.add(mapGoods);
        }
        map.put("orderDetails", goodsList);
        map.put("shipPhone", tvUserPhone.getText().toString());
        map.put("shipName", tvUserName.getText().toString());
        map.put("shipAddr", tvAddress.getText().toString());
        JSONRequest.getInstance().sendRequest(JSONRequest.POST, ConfirmOrderURL.RENT_ORDER, map, handler, ADD_RENT_ORDER, HttpUtil.ACTION_JSON);
    }

    private void setAdapter() {
        isShopping = ((CurryApplication) this.getApplicationContext()).getShopping();
        if (isShopping) {
            goodsInfoList = GoodsDao.getInstance().selectUserInfoList();
            totalPrice = GoodsDao.getInstance().getGoodsTotalPrice();
        } else {
            goodsInfoList = new ArrayList<>();
            goodsInfoList.add((GoodsInfo) getIntent().getParcelableExtra(GOODS_INFO));
            totalPrice = goodsInfoList.get(0).getUnitPrice();
        }
        tvPayPrice.setText(Constant.RMB_FLAG + totalPrice);
        goodsRentOrderAdapter = new GoodsRentOrderAdapter(goodsInfoList);
        lvGoodsRent.setAdapter(goodsRentOrderAdapter);
        goodsRentOrderAdapter.notifyDataSetChanged();
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            DialogHelper.closeDialog(dialog);
            switch (msg.what) {
                case ADD_RENT_ORDER:
                    parseJsonData(msg);
                    jumpUserPaidActivity();
                    break;
                case Constant.WHAT_TOKEN_EXPIRED:
                    ToastHelper.getInstance()._toast(msg.obj.toString());
                    break;
                case Constant.WHAT_ERROR_HTTP:
                    ToastHelper.getInstance()._toast(msg.obj.toString());
                    break;
            }
        }
    };

    private void parseJsonData(Message msg) {
        try {
            JSONObject jsonObject = new JSONObject(msg.obj.toString());
            goodsMap = JSONHelper.jsonToMap(jsonObject.get("entity").toString());
            payMap = JSONHelper.jsonToMap(goodsMap.get("crePay").toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void jumpUserPaidActivity() {
        String orderID = String.valueOf(goodsMap.get("orderID"));
        String paidMethod = PaymentModule.mode[10];
        String totalCost = String.valueOf(payMap.get("totalCost"));
        String orderContent = String.valueOf(payMap.get("orderContent"));
        String outtradeno = String.valueOf(payMap.get("outtradeno"));
        PayJumper.jumpToUsersPaid(this, orderID, paidMethod, totalCost, orderContent, outtradeno, null, null);
        ExitApplication.getInstance().addMainActivity(GoodsRentConfirmOrderActivity.this);
        finish();
    }

    private View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.tvPay:
                    addGoodsRentRequset();
                    break;
            }
        }
    };
}
